package com.cheyunkeji.er.view.evaluate;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Michael on 2017/7/7 16:17.
 */

public class BCSMCheckItemView extends LinearLayout implements CompoundButton.OnCheckedChangeListener {

    private static final String TAG = BCSMCheckItemView.class.getSimpleName();

    //    Item下标
    private  int checkItemIndexNumber = -1 ;

    @BindView(R.id.tv_index_number)
    TextView tvIndexNumber;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    @BindView(R.id.cb_lite)
    CheckBox cbLite;
    @BindView(R.id.cb_heavy)
    CheckBox cbHeavy;
    @BindView(R.id.cb_fix)
    CheckBox cbFix;
    @BindView(R.id.ll_cbs_group)
    LinearLayout llCbsGroup;
    @BindView(R.id.ll_content)
    LinearLayout rootView;
    private CheckBox[] boxs;

    private int currentCheckedIndex = 3;

    public BCSMCheckItemView(Context context) {
        super(context);
        initView();
    }

    public BCSMCheckItemView(Context context, AttributeSet attrs) {
        super(context, attrs);

        rootView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.er_layout_bcsm_item, this, true);
        ButterKnife.bind(this, rootView);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BCSMCheckItemView);

        for (int i = 0; i < typedArray.getIndexCount(); i++) {

            switch (typedArray.getIndex(i)) {
                case R.styleable.BCSMCheckItemView_bcsm_check_bkg_color:
                    rootView.setBackgroundColor(typedArray.getColor(typedArray.getIndex(i), Color.parseColor("#00000000")));
                    break;
                case R.styleable.BCSMCheckItemView_bcsm_check_desc:
                    tvDesc.setText(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.BCSMCheckItemView_bcsm_check_index_number:
                    tvIndexNumber.setText(typedArray.getString(typedArray.getIndex(i)));
                    checkItemIndexNumber = Integer.valueOf(typedArray.getString(typedArray.getIndex(i)));
                    break;
                case R.styleable.BCSMCheckItemView_bcsm_check_text_category:

                    if(typedArray.getInt(typedArray.getIndex(i),1) == 2){

                        cbLite.setText("缺件");
                        cbHeavy.setText("损坏");
                        cbFix.setText("可修理");

                    }
                    break ;

                default:
                    break;
            }
        }

        typedArray.recycle();

        initView();
    }

    public BCSMCheckItemView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);

    }

    public int getIndexId() {
        return checkItemIndexNumber;
    }

    private void initView() {

        boxs = new CheckBox[]{cbLite, cbHeavy, cbFix};

        for (int i = 0; i < boxs.length; i++) {
            boxs[i].setOnCheckedChangeListener(this);
        }

    }

    public int getCurrentCheckedIndex() {
        return currentCheckedIndex;
    }

    /**
     * 选中某项
     *
     * @param index
     */
    public void setCheckedIndex(int index) {

        if (3 == index) {
            for (int i = 0; i < boxs.length; i++) {
                if (boxs[i].isChecked())
                    boxs[i].setChecked(false);
            }
        } else {
            boxs[index].setChecked(true);
        }
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {

            for (int i = 0; i < boxs.length; i++) {
                if (buttonView == boxs[i]) {
                    currentCheckedIndex = i;
                }
            }

            for (int i = 0; i < boxs.length; i++) {
                if (currentCheckedIndex != i && boxs[i].isChecked()) {
                    boxs[i].setChecked(false);
                }
            }

            if(null != listener)
            listener.onCheckChanged(checkItemIndexNumber,currentCheckedIndex);

        } else {
            if (buttonView == boxs[currentCheckedIndex]) {
                currentCheckedIndex = 3;
                if(null != listener)
                listener.onCheckChanged(checkItemIndexNumber,currentCheckedIndex);
            }
        }
    }

    public void setDesc(String desc){
        tvDesc.setText(desc);
    }

    public void setIndexNumber(String desc){
        tvIndexNumber.setText(desc);
    }


    public void setListener(OnCheckChangeListener listener) {
        this.listener = listener;
    }

    private OnCheckChangeListener listener ;


    public interface OnCheckChangeListener{

        void onCheckChanged(int itemViewIndex, int checkBoxIndex );

    }

}
